package com.white.loggerserver.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import androidx.recyclerview.widget.RecyclerView
import com.white.loggerserver.R
import kotlin.properties.Delegates

/**
 * Created by ytf on 2020/3/1 001.
 * Description:
 */
class BorderStyleRecycleView : RecyclerView {

    var borderColor = Color.parseColor("#E2E2E2")
        set(value) {
            field = value
            mPaint.color = value
        }

    var borderWidth = 1f
        set(value) {
            field = value
            mPaint.strokeWidth = value
        }

    private val mPaint : Paint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG)
    }

    constructor(context: Context) : this(context, null) {}

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr){

        setWillNotDraw(false)
        attrs?.apply {
            val ta = context.obtainStyledAttributes(this, R.styleable.CommonDaBox);
            borderColor = ta.getColor(R.styleable.CommonDaBox_cdb_border_color, Color.parseColor("#3f3f3f"))
            borderWidth = ta.getDimension(R.styleable.CommonDaBox_cdb_border_width, 1f)
            ta.recycle()
        }

        mPaint.style = Paint.Style.STROKE
        mPaint.color = borderColor
        mPaint.strokeWidth = borderWidth

//        val iconfont = Typeface.createFromAsset(context.assets, "songti.ttf")
//        typeface = iconfont
    }


    override fun dispatchDraw(canvas: Canvas?) {
        super.dispatchDraw(canvas)
        val half = borderWidth / 2
        canvas!!.drawRect(half, half, width - half, height - half, mPaint)
    }
}